//
//  ViewController.m
//  CAKeyframeAnimationPath
//
//  Created by Xwoder on 17/1/29.
//  Copyright © 2017年 Xwoder. All rights reserved.
//

#import "ViewController.h"
#import "UIView+ALQuickFrame.h"

@interface ViewController ()

@property (nonatomic, weak) UIView *orangeView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *orangeView = [[UIView alloc] init];
    orangeView.backgroundColor = [UIColor orangeColor];
    orangeView.size = CGSizeMake(50, 50);
    orangeView.center = CGPointMake(25, 25);
    [self.view addSubview:orangeView];
    self.orangeView = orangeView;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // 路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGFloat x1 = self.orangeView.center.x;
    CGFloat y1 = self.orangeView.center.y;
    CGPathMoveToPoint(path, NULL, x1, y1);
    CGFloat endX = self.view.width - self.orangeView.width / 2;
    CGFloat endY = self.view.height - self.orangeView.height / 2;
    CGPathAddLineToPoint(path, NULL, endX, endY);

    // 关键帧动画
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"position";
    animation.path = path;
    animation.duration = 2;
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    
    [self.orangeView.layer addAnimation:animation forKey:nil];
}

@end
